﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace PingPongClient
{
    class Program
    {
        static int count = 0;
        static int err = 0;
        static int start = 1;
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                start = int.Parse(args[0]);
            }
            Thread t = null;
            for (int i = 0; i < 10000; i++)
            {
                try
                {
                    t = new Thread(new ThreadStart(ThreadBody));
                    t.Start();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Thread " + i + " err on " + ex.Message);
                }
            }
            Console.Read();
        }
        static void ThreadBody()
        {
            Interlocked.Increment(ref count);
            try
            {
                bool work = true;
                TcpClient tc = new TcpClient(
                    new IPEndPoint(
                        IPAddress.Parse("192.168.87.53"), (10000*start + count))
                        );
                tc.Connect(
                    new IPEndPoint(
                        IPAddress.Parse("192.168.87.53"), 8888)
                        );

                Console.WriteLine(count + " Connected");
                while (work)
                {
                    NetworkStream ns = tc.GetStream();
                    byte[] data = Encoding.UTF8.GetBytes("Ping...........");
                    if (tc.Connected)
                    {
                        ns.Write(data, 0, data.Length);
                        //Console.WriteLine("send Ping.....");
                    }
                    else
                    {
                        work = false;
                        Console.WriteLine("server drop connection");
                    }
                    //Console.WriteLine("Ping send");
                    byte[] buffer = new byte[1024];
                    int rc = 0;
                    MemoryStream ms = new MemoryStream();
                    if (tc.Connected)
                    {
                        while ((rc = ns.Read(buffer, 0, 1024)) > 0)
                        {
                            ms.Write(buffer, 0, rc);
                            if (rc < 1024)
                            {
                                break;
                            }
                        }
                        //Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
                    }
                    else
                    {
                        work = false;
                        Console.WriteLine("server drop connection");
                    }
                    //Console.Write(Encoding.UTF8.GetString(ms.ToArray()));
                    //Console.Read();
                    Thread.Sleep(10);
                }
                tc.Close();
            }
            catch (Exception ex)
            {
                Interlocked.Increment(ref err);
                Console.WriteLine("err:" + err);
            }
        }
    }
}
